/** @jsxImportSource @emotion/react */
import * as React from "react";
import {
    Table, TableBody, TableRow, CCell, TableHead, Text, useTheme, Button,
} from "customize-easy-ui-component";
import {DirectLink, Link as RouterLink,} from "../../../routing/Link";
import {ReportViewProps,} from "../../common/base";
import {末尾链接, 落款单位地址,} from "../../common/rarelyVary";
import {报告设备详情, } from "./repView";
import {useRepMenuDirItems, } from "../../hook/useMainRepUrlOr";
import {setupItemAreaRoute} from "./orcIspConfig";
import queryString from "query-string";
import {FormatOriginal, } from "./FormatOriginal";
import RoutingContext from "../../../routing/RoutingContext";
import {useContext} from "react";
import {ReportFirstPageHeadSimpNQR, } from "../../safe/rarelyVary";
import {useItemsMapOmni} from "../../common/omni";
import {Column_Setting} from "../../common/useFormatOmni";
import {useOfficialOmni, } from "../../common/useOfficialOmni";
import {UnqualifiedIspTable} from "../../common/general";
import {首页概况AmusD} from "../viewFirst";
import {注意事项AmusD} from "../viewAttention";

export const ReportView: React.FunctionComponent<ReportViewProps> = ({
repId,  source: orc,  verId,rep,
}) => {
    const qs= queryString.parse(window.location.search);
    const formatOriginal =qs && !!qs.original;      //改成  格式化版原始记录
    const { history } = useContext(RoutingContext);
    const Component=formatOriginal? FormatOriginal : OfficialReport;
    return (<>
        <Component  source={orc} verId={verId} repId={repId} rep={rep}/>
        <div css={{margin: '0.5rem', "@media print": {display: 'none'} }}>
            <Button intent="danger" variant="outline"
                  onPress={async () => {
                      qs.original =formatOriginal? '' : '1';
                      history.location.search = queryString.stringify(qs);
                      const toUrl= history.createHref(history.location);
                      history.push(toUrl);
                  }}>{formatOriginal? '正式报告':'格式化版原始记录'}
            </Button>
        </div>
    </>);
}
// {floatInterception(orc?.['轿顶间v'] - orc?.['空高差H'], 3)}m
const 检验结果替换 =((orc: { [x: string]: any; }) => {
    let out={...orc};
    if(undefined!==orc?.['厢周安距v'])  out['吊厢安距']=<div>{orc?.['厢周安距v']}mm</div>;
    if(undefined!==orc?.接地电阻v)  out['配接地阻']=<div>{orc?.接地电阻v}Ω</div>;
    if(undefined!==orc?.回路地阻v)  out.回路绝缘=<div>{orc?.回路地阻v}MΩ</div>;
    return out;
});
const config报告 : Column_Setting[]=[{n:'',x:'检验结果',}, {n:null,x:'结论'}];
const JumpTags=[{name:'设备概况',ha:'Survey'},{name:'1.资料审查',ha:'1.4'},
         {name:'7.电气及控制',ha:'7.1'},{name:'现场检验条件确认',ha:'SiteCondition'}];

const OfficialReport: React.FunctionComponent<ReportViewProps> = ({
    repId,   source: orc,  verId,rep,
}) => {
    const qs= queryString.parse(window.location.search);
    const printing =qs && !!qs.print;
    const theme= useTheme();
    const [rootMenu]=useRepMenuDirItems(JumpTags, [],`/reportView/${rep?.modeltype}/ver/${rep?.modelversion}/${rep?.id}`);
    //【特殊部分】orc?._Oitems: 动态，用户定制的；
    const impressionismAs =React.useMemo(() => {
        return setupItemAreaRoute({rep,orc, theme});
    }, [verId, repId,rep, orc?._Oitems, theme]);
    const {renderIspContent} =useOfficialOmni({orc,ItemArs:impressionismAs?.Item, rep, config:config报告, itResCB:检验结果替换});
    const [mapNoTag]=useItemsMapOmni({ ItemArs:impressionismAs?.Item, notCheckNo:false});
  return (
    <React.Fragment>
      {rootMenu}
       <div css={{"@media not print": {marginTop: '1rem', marginBottom: '1rem'}}}>
            <div css={{"@media print": {height: '100vh'}}}>
                {ReportFirstPageHeadSimpNQR({theme, rep, mbbm: 'FJB/TC-1001-1-0-2023'})}
                <div css={{
                    "@media print": {
                        display: 'flex',
                        flexDirection: 'column',
                        justifyContent: 'space-between',
                        height: 'calc(100vh - 125px)',
                    }
                }}>
                    <Text variant="h3" css={{textAlign: 'center', "@media print": {fontSize: theme.fontSizes[5], marginTop: '1.5rem',},}}>
                        观览车类大型游乐设施定期检验报告
                    </Text>
                    {首页概况AmusD({theme, orc, rep})}
                    <div css={{textAlign: 'center', "@media print": {pageBreakAfter: 'always', pageBreakInside: 'avoid'}}}>
                        {落款单位地址}
                    </div>
                </div>
            </div>
           {注意事项AmusD({
               rep,
               comply: '依据《游乐设施监督检验规程（试行）》（国质检锅[2002]124号），适用于观览车类大型游乐设施的定期检验'
           })}
           <DirectLink href={`/report/${rep?.modeltype}/ver/${verId}/${repId}/Instrument`}>
               <div css={{
                   display: 'flex',
                   flexDirection: 'column',
                   justifyContent: 'center',
                   "@media print": {
                       pageBreakBefore: 'always',
                   },
               }}>
                   <Text variant="h4">一、设备概况</Text>
               </div>
               <div css={{display: 'flex', justifyContent: 'space-between'}}>
                   <Text></Text>
                   <Text>报告编号：{rep.isp.no}</Text>
               </div>
           </DirectLink>
           {报告设备详情({theme, orc, rep})}
           <RouterLink href={`/report/${rep?.modeltype}/ver/${verId}/${repId}/_OtherItem#_OtherItem`}>
               <Text variant="h4" css={{"@media print": {display: 'none'}}}>检验项目: 10 其它项目的设置</Text>
           </RouterLink>
            <div css={{"@media print": {paddingBottom: '13.3rem', pageBreakInside: 'avoid'}}}>
                <div css={{display: 'flex', justifyContent: 'space-between',minHeight:'1rem'}}>
                    <Text></Text><Text></Text>
                </div>
            </div>
            <Table fixed={ ["3.1%","8%","7.2%","3%","3%","%","18%","7%"] } tight miniw={800}
                          css={{borderCollapse: 'collapse', "@media print": {marginTop: '-13.3rem'}}}>
                    <TableHead>
                        <DirectLink href={`/report/${rep?.modeltype}/ver/${verId}/${repId}/ALL`}>
                            <TableRow>
                                <CCell><Text css={{fontSize:'0.65rem'}}>序号</Text></CCell><CCell>项目</CCell><CCell colSpan={3}><Text css={{fontSize:'0.8rem'}}>项目编号</Text></CCell>
                                <CCell colSpan={1}>检验项目及内容</CCell><CCell>检验结果</CCell><CCell>结论</CCell>
                            </TableRow>
                        </DirectLink>
                    </TableHead>
                <TableBody>
                {renderIspContent}
                </TableBody>
            </Table>
           <Text css={{fontSize:'0.8rem'}}>
               注：※项为重要项；其它项为一般项。
           </Text>
           <UnqualifiedIspTable rep={rep} orc={orc} mapNoTag={mapNoTag} printing={printing} titles={['序号','类别/编号','检验不符合内容描述','复检结论','确认日期']}
                                label={<Text variant="h4">检验不符合记录及整改情况确认</Text>}/>
        </div>
        <div>
            <RouterLink href={`/report/${rep?.modeltype}/ver/${verId}/${repId}/Instrument#Instrument`}>
                <Text variant="h4" css={{"@media print": {display: 'none'}}}>主要测量设备性能检查</Text>
            </RouterLink>
            <RouterLink href={`/report/${rep?.modeltype}/ver/${verId}/${repId}/Witness#Witness`}>
                <Text variant="h4" css={{"@media print": {display: 'none'}}}>见证材料 、备注</Text>
            </RouterLink>
            <RouterLink href={`/report/${rep?.modeltype}/ver/${verId}/${repId}/SiteCondition#SiteCondition`}>
            <Text id={'SiteCondition'} variant="h4" css={{"@media print": {display: 'none'}}}>附录：现场检测条件确认</Text>
            </RouterLink>
        </div>
      {末尾链接({template:rep?.modeltype,verId, repId: repId||''})}
    </React.Fragment>
  );
}
